package com.example.myapplication.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.utils.HDWan_Item_Util;

import java.util.List;

/**
 * Created by Lenovo-HD on 2016/5/19.
 */
public class HDWanAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater mLayoutInflater;
    private List<HDWan_Item_Util> mHDWan_item_utils;

    public HDWanAdapter(Context context, List<HDWan_Item_Util> mHDWan_item_utils) {
        mLayoutInflater = LayoutInflater.from(context);
        this.mHDWan_item_utils = mHDWan_item_utils;
        this.context = context;
    }

    public void addFirst(HDWan_Item_Util hdWan_item_util) {
        mHDWan_item_utils.add(0, hdWan_item_util);
    }

    @Override
    public int getCount() {
        return mHDWan_item_utils.size();
    }

    @Override
    public Object getItem(int position) {
        return mHDWan_item_utils.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HDWan_Item_Util hdWan_item_util = mHDWan_item_utils.get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.hdwan_listitem, null);
            viewHolder = new ViewHolder();
            viewHolder.hdWan_item_image = (ImageView) convertView.findViewById(R.id.hdWan_item_image);
            viewHolder.hdWan_item_filmName = (TextView) convertView.findViewById(R.id.hdWan_item_fileName);
            viewHolder.hdWan_item_filmCategory = (TextView) convertView.findViewById(R.id.hdWan_item_filmCategory);
            viewHolder.mNetworkImageView = (NetworkImageView) convertView.findViewById(R.id.hdWan_item_networkImageView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //使用glide加载图片比volley networkImageView快很多，volley的loadImage去加载图片会有一个默认图片，看起来不是很友好
        Glide.with(context).load(hdWan_item_util.getImg_url()).into(viewHolder.hdWan_item_image);
        //NetworkImageViewVolley(hdWan_item_util.getImg_url(), viewHolder.mNetworkImageView);
        //loadImageVolley(hdWan_item_util.getImg_url(),viewHolder.hdWan_item_image);
        viewHolder.hdWan_item_filmName.setText(hdWan_item_util.getFilmName().toString());
        viewHolder.hdWan_item_filmCategory.setText(hdWan_item_util.getFilmCategory().toString());
        //viewHolder.hdWan_item_filmCategory.setText("分类");
        Log.i("info", hdWan_item_util.getFilmName().toString());

        return convertView;
    }

    //定义一个viewHolder
    class ViewHolder {
        NetworkImageView mNetworkImageView;
        ImageView hdWan_item_image;
        TextView hdWan_item_filmName;
        TextView hdWan_item_filmCategory;

    }

    //使用 Volley加载图片
    public void loadImageVolley(String url, ImageView imageview) {
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        final LruCache<String, Bitmap> lruCache = new LruCache<>(20);
        ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String s) {
                return lruCache.get(s);
            }

            @Override
            public void putBitmap(String s, Bitmap bitmap) {
                lruCache.put(s, bitmap);
            }
        };
        ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
        ImageLoader.ImageListener listener = imageLoader.getImageListener(imageview, R.mipmap.loading, R.mipmap.loading);
        imageLoader.get(url, listener);
    }

    /**
     * 使用NetworkImageView加载图片
     */
    public void NetworkImageViewVolley(String url, NetworkImageView networkImageView) {
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        final LruCache<String, Bitmap> lruCache = new LruCache<>(20);
        ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String s) {
                return lruCache.get(s);
            }

            @Override
            public void putBitmap(String s, Bitmap bitmap) {
                lruCache.put(s, bitmap);
            }
        };
        ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
        networkImageView.setTag("url");
        networkImageView.setImageUrl(url, imageLoader);
    }

}
